package com.teach.controller.shop;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.teach.dto.LoginUser;
import com.teach.entity.Product;
import com.teach.entity.ShopAddress;
import com.teach.entity.ShopCart;
import com.teach.entity.ShopOrder;
import com.teach.entity.ShopOrderItem;
import com.teach.mapper.ProductMapper;
import com.teach.mapper.ShopAddressMapper;
import com.teach.mapper.ShopCartMapper;
import com.teach.mapper.ShopOrderItemMapper;
import com.teach.mapper.ShopOrderMapper;
import com.teach.service.shop.ShopAddressService;
import com.teach.service.shop.ShopGoodsCartService;
import com.teach.service.shop.ShopGoodsService;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class ShopOrderTiJiaoController {
	
	@Autowired
	private ShopCartMapper mapper;
	@Autowired
	private ShopAddressMapper mapper1;
	@Autowired
	private ShopOrderMapper mapper2;
	@Autowired
	private ShopOrderItemMapper mapper3;
	@Autowired
	private ShopAddressService service1;
	@Autowired
	private ShopGoodsService service;
	@Autowired
	private  ShopGoodsCartService service2;
	@Autowired
	private  ProductMapper promapper;
	
	
	//购物车到订单提交
	@PostMapping("/tijiao.cart")
	public String doShopMain(Model model,String cartItemIds,LoginUser user,String total) {
		String[] su=cartItemIds.split(",");
		for (int i = 0; i < su.length; i++) {
			log.debug("{}",su[i]);
		}
		
		Long [] lo=new Long[su.length];
		for (int i = 0; i < su.length; i++) {
			 lo[i] = Long.parseLong(su[i]);
		}
		
		List<ShopCart> list = new ArrayList<ShopCart>();
		ShopCart pro=null;
		for (int i = 0; i < lo.length; i++) {
			pro=mapper.selectByprouctId(lo[i],user.getUserName());
			list.add(pro);
		}
		model.addAttribute("addressList", service1.get(user.getUserName()));
		model.addAttribute("product", list);
		model.addAttribute("total", total);
		return "shop/order";
	}
	
	
	@PostMapping("/zhifubao.do")
	@Transactional
	public String dingdan(Long dindanId,Model model,LoginUser user,Integer dizhi,Double price,String goodsname,String shangpID,String liuyanxx,String bianhao,String orderid) {
		if(orderid==null||orderid.equals("")) {
			String[] su=shangpID.split(",");
			Long [] lo=new Long[su.length];
			for (int i = 0; i < su.length; i++) {
				 lo[i] = Long.parseLong(su[i]);
			}
			
			Long [] dindanhao=new Long[su.length];
			for (int i = 0; i < su.length; i++) {
				dindanhao[i] = Long.valueOf(sjs());
			}
			
			String r=Arrays.toString(dindanhao);
			String str=r.substring(1, r.length()-1);
			
			String[] shuliang=bianhao.split(",");
			Integer [] shuliang1=new Integer[shuliang.length];
			for (int i = 0; i < shuliang.length; i++) {
				shuliang1[i] = Integer.valueOf(shuliang[i]);
			}
			Double [] shuliang2=new Double[shuliang.length];
			for (int i = 0; i < shuliang.length; i++) {
				shuliang2[i] = Double.parseDouble(shuliang[i]);
			}
			
			ShopAddress address=mapper1.selectByPrimaryKey(dizhi);
			String dz=address.getShopAddressProvince()+"-"+address.getShopAddressCity()+"-"+address.getShopAddressArea()+"-"+address.getShopAddressUserAddress();
			
			for (int i = 0; i < lo.length; i++) {
				Product p=service.get(lo[i]);
				ShopOrder sorder=new ShopOrder();
				
				sorder.setShopOrderId(dindanhao[i]);
				sorder.setShopOrderUserName(user.getUserName());
				sorder.setShopOrderProductId(lo[i]);
				sorder.setShopOrderTotalPrice(shuliang2[i]*p.getProductPrice1());
				sorder.setShopOrderCreatTime(new Date());
				sorder.setShopOrderState(1);
				
				ShopOrderItem itme=new ShopOrderItem();
				
				itme.setShopOrderItemUserName(user.getUserName());
				itme.setShopOrderItemProductId(lo[i]);
				itme.setShopOrderItemProductName(p.getProductName());
				itme.setShopOrderItemProductPrice(p.getProductPrice1());
				itme.setShopOrderItemNum(shuliang1[i]);
				itme.setShopOrderItemTotalPrice(shuliang2[i]*p.getProductPrice1());
				itme.setShopOrderId(dindanhao[i]);
				itme.setShopOrderItemAddress(dz);
				itme.setShopOrderItemCreatTime(sorder.getShopOrderCreatTime());
				itme.setShopOrderItemState(sorder.getShopOrderState());
				itme.setShopOrderPhone(address.getShopAddressUserTel());
				itme.setShopOrderRealName(address.getShopAddressRealName());
				itme.setShopOrderItemRemarks(liuyanxx);
				
				mapper2.insertSelective(sorder);
				mapper3.insert(itme);
				service2.deleteCart(lo[i], user.getUserName());
			}
			model.addAttribute("q1", str);//订单编号-用于支付传参
			model.addAttribute("q3", goodsname);//订单名称-用于支付传参
			model.addAttribute("q2", price.toString());//订单价格-用于支付传参
			model.addAttribute("q4", bianhao);//订单描述-订单备注-用于支付传参
		}else {
			ShopAddress address=mapper1.selectByPrimaryKey(dizhi);
			String dz=address.getShopAddressProvince()+"-"+address.getShopAddressCity()+"-"+address.getShopAddressArea()+"-"+address.getShopAddressUserAddress();
			String id=sjs();
			ShopOrderItem itme=new ShopOrderItem();
			itme.setShopOrderItemAddress(dz);
			itme.setShopOrderItemRemarks(liuyanxx);
			itme.setShopOrderId(Long.valueOf(id));
			itme.setShopOrderItemId(dindanId);
			mapper3.updateByPrimaryKeySelective(itme);
			mapper2.update(Long.valueOf(id), Long.valueOf(orderid));
			model.addAttribute("q1",Long.valueOf(id).toString());//订单编号-用于支付传参
			model.addAttribute("q3", goodsname);//订单名称-用于支付传参
			model.addAttribute("q2", price.toString());//订单价格-用于支付传参
			model.addAttribute("q4", liuyanxx);//订单描述-订单备注-用于支付传参
			model.addAttribute("q5", "1,1");//数量-订单备注-用于支付传参
		}
		return "shop/shop-pay";//跳转支付
	}
	
	@GetMapping("/zhifubao2.do")
	@Transactional
	public String dingdan2(String out_trade_no,String trade_no) {
		String[] su=out_trade_no.split(", ");
		for (int i = 0; i < su.length; i++) {
			ShopOrder order =new ShopOrder();
			order.setShopOrderAliId(trade_no);
			order.setShopOrderId(Long.valueOf(su[i]));
			mapper2.updateByPrimaryKeySelective(order);
			mapper3.updateByoid(new Date(), Long.valueOf(su[i]));
			mapper3.updates(2, Long.valueOf(su[i]));
			ShopOrderItem s=mapper3.selectById(Long.valueOf(su[i]));
			Product pro=promapper.selectB(s.getShopOrderItemProductId());
			Integer xiaoliang=pro.getProductSales();
			Integer kucun=pro.getProductInventory();
			Product p=new Product();
			p.setProductId(s.getShopOrderItemProductId());
			p.setProductSales(xiaoliang+s.getShopOrderItemNum());
			p.setProductInventory(kucun-s.getShopOrderItemNum());
			service.update(p);
		}
		return "shop/orderState";//跳转支付
	}
	
	
	
	/**
	 * 生成订单号  当前时间加上两位随机数
	 * @return
	 */
	private String sjs() {
		String sjs = "";
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		int x=(int)(Math.random()*100);
		if(x<10){
			x=x+9;
		}else if(x==100){
			x--;
		}
		sjs=sdf.format(d)+x;
		return sjs;
	}
}
